package window.tab;

import entity.FlightInfo;
import entity.HotelInfo;
import manage.DBManager;
import manage.FlightDBManager;
import util.CityCache;
import window.component.FlightCard;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Set;

public class FlightTab extends JPanel {
    private JComboBox<String> departureComboBox;
    private JComboBox<String> destinationComboBox;
    private JSpinner dateSpinner;
    private JButton searchButton;
    ArrayList<FlightInfo> flightList = new ArrayList<>();  // 缓存的机票列表

    public FlightTab() {
        JLabel titleLabel = new JLabel("Flight Booking");
        titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);

        JLabel departureLabel = new JLabel("Departure:");
        departureLabel.setFont(new Font("Arial", Font.PLAIN, 16));
        Set<String> citySet = CityCache.getCitiesOfHotels();
        ArrayList<String> citiesArray = new ArrayList<>();
        citiesArray.add("All Cities");
        citiesArray.addAll(citySet);
        String[] cities = citiesArray.toArray(new String[0]);
        departureComboBox = new JComboBox<>(cities);
        departureComboBox.setFont(new Font("Arial", Font.PLAIN, 16));

        JLabel destinationLabel = new JLabel("Destination:");
        destinationLabel.setFont(new Font("Arial", Font.PLAIN, 16));
        destinationComboBox = new JComboBox<>(cities);
        destinationComboBox.setFont(new Font("Arial", Font.PLAIN, 16));

        JLabel dateLabel = new JLabel("Departure Date:");
        dateLabel.setFont(new Font("Arial", Font.PLAIN, 16));
        SpinnerDateModel dateModel = new SpinnerDateModel(new Date(), null, null, java.util.Calendar.DAY_OF_MONTH);
        dateSpinner = new JSpinner(dateModel);
        JSpinner.DateEditor dateEditor = new JSpinner.DateEditor(dateSpinner, "yyyy-MM-dd");
        dateSpinner.setEditor(dateEditor);
        dateSpinner.setFont(new Font("Arial", Font.PLAIN, 16));

        searchButton = new JButton("Search Flights");
        searchButton.setFont(new Font("Arial", Font.PLAIN, 16));
        searchButton.addActionListener(new SearchButtonListener());

        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        add(titleLabel, gbc);

        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridBagLayout());
        GridBagConstraints inputGbc = new GridBagConstraints();
        inputGbc.insets = new Insets(5, 5, 5, 5);
        inputGbc.fill = GridBagConstraints.HORIZONTAL;

        inputGbc.gridx = 0;
        inputGbc.gridy = 0;
        inputPanel.add(departureLabel, inputGbc);

        inputGbc.gridx = 1;
        inputGbc.gridy = 0;
        inputPanel.add(departureComboBox, inputGbc);

        inputGbc.gridx = 0;
        inputGbc.gridy = 1;
        inputPanel.add(destinationLabel, inputGbc);

        inputGbc.gridx = 1;
        inputGbc.gridy = 1;
        inputPanel.add(destinationComboBox, inputGbc);

        inputGbc.gridx = 0;
        inputGbc.gridy = 2;
        inputPanel.add(dateLabel, inputGbc);

        inputGbc.gridx = 1;
        inputGbc.gridy = 2;
        inputPanel.add(dateSpinner, inputGbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        add(inputPanel, gbc);

        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.gridwidth = 2;
        add(searchButton, gbc);
    }

    private class SearchButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String departure = (String) departureComboBox.getSelectedItem();
            String destination = (String) destinationComboBox.getSelectedItem();
            Date date = (Date) dateSpinner.getValue();
            String dateString = new SimpleDateFormat("yyyy-MM-dd").format(date);

            // Simulate searching for flights that match the criteria
            ArrayList<FlightInfo> flights = new ArrayList<>();

            // Adding sample flight info (ensure all fields are properly populated)
            flightList=FlightDBManager.getFlight(departure,destination);
            for (FlightInfo flightInfo:flightList){
                flights.add(flightInfo);
            }
            // Create a card layout for displaying flight details
            JPanel flightPanel = new JPanel();
            flightPanel.setLayout(new BoxLayout(flightPanel, BoxLayout.Y_AXIS));
            flightPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

            for (FlightInfo flight : flights) {
                if (flight != null) {
                    flightPanel.add(new FlightCard(flight));
                    flightPanel.add(Box.createRigidArea(new Dimension(0, 10))); // add some spacing between cards
                }
            }

            // Display available flights in a dialog with card-style layout
            JFrame flightFrame = new JFrame("Available Flights");
            flightFrame.setSize(600, 400);
            flightFrame.setLocationRelativeTo(null);
            flightFrame.add(new JScrollPane(flightPanel));
            flightFrame.setVisible(true);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("Flight Booking Page");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(600, 400);
            frame.setLocationRelativeTo(null);
            frame.add(new FlightTab());
            frame.setVisible(true);
        });
    }
}